<?php

namespace App\Console\Commands\Tools\Kms;

use AlibabaCloud\SDK\Green\V20220302\Models\TextModerationRequest;
use App\Library\Guanyuan\GuanyuanTool;
use App\Models\Common\ConfigModel;
use Guanguans\Notify\Factory;
use Guanguans\Notify\Messages\FeiShu\CardMessage;
use Guanguans\Notify\Messages\FeiShu\TextMessage;
use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Request;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Http;
use App\Console\Commands\Traits\ExcelCommandTrait;
use Illuminate\Console\Command;

class KmsDataDiffCommand extends Command
{
    protected $signature = 'tools:kms:kms-data-diff {--type=1}';
    use ExcelCommandTrait;

    public function handle()
    {
        $type = (int)$this->option('type');

        switch ($type) {
            case 1:
                // 正常营业
                $title       = '正常营业';
                $cacheKey    = 'tools:kms:kms-data-diff-zhengchang_2';
                $gyFilterOne = [
                    'name'        => '状态',
                    'filterType'  => 'EQ',
                    'filterValue' => ['正常营业']
                ];
                $queryType   = '8';
                break;

            case 2:
                // 暂停营业
                $title       = '暂停营业';
                $cacheKey    = 'tools:kms:kms-data-diff-zanting_2';
                $gyFilterOne = [
                    'name'        => '状态',
                    'filterType'  => 'EQ',
                    'filterValue' => ['暂停营业']
                ];
                $queryType   = '9';
                break;
            case 3:
                // 暂停营业
                $title       = '筹备中';
                $cacheKey    = 'tools:kms:kms-data-diff-choubei_2';
                $gyFilterOne = [
                    'name'        => '状态',
                    'filterType'  => 'EQ',
                    'filterValue' => ['筹备中']
                ];
                $queryType   = '4,5,6';
                break;
            case 4:
                // 选址中
                $title       = '选址中';
                $cacheKey    = 'tools:kms:kms-data-diff-xuanzhi_2';
                $gyFilterOne = [
                    'name'        => '状态',
                    'filterType'  => 'EQ',
                    'filterValue' => ['选址中']
                ];
                $queryType   = '1,2,3';
                break;
        }

        $data = Cache::remember($cacheKey, 3600, function () use ($queryType) {
            $client  = new Client();
            $headers = [
                'Authorization' => 'Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJhdWQiOiIzIiwianRpIjoiMmNiNTE3NDEzN2VmM2Q3NGM3ZmFkNThiNmYzYzA4NWMwZmIyZTc5MzFiYzVkODIwNDAyYTQ4YjQxN2ZhMjk2NjY2ZWM5MTAyNTQwY2RjZGYiLCJpYXQiOjE2OTk0MDkzMDMsIm5iZiI6MTY5OTQwOTMwMywiZXhwIjoxNzAyMDAxMzAyLCJzdWIiOiI1ODQxIiwic2NvcGVzIjpbIioiXSwiaXAiOiI1OC4zNC43NS4yMjYiLCJ1c2VyX2lkIjoiNTg0MSIsInR5cGUiOiJrbXMifQ.YcciUVzbcSNnY2ORIbzHsn7iSluR56hHAT4cby9n2rB4XPTpIrhBCSqO-vnwv2OsEUmEylGZM8Sex6POm8bJEV8xbk7w6Fc0CO7XnROQsQvGKzMyeKM7ThpyCY5Hs_83ROtG0_1DJu08jkQJ0tSq1MozwP8ISjNxbsUnDzJ-Wflr2PkjIXAoP58EQDqX9gWOjyKp8QBgK2vhY3U024qP4eWFeWWk3rJzRJnVMSXmx6bN1PNf7jWYT32-FnUAJJIpFLbpySOSphZGZ09R8OuOGE9_Txw_MzODXk8DnLwsZIdjHHTCjnZtmBrtDrI6mvCb6xtF0_m7BUvyOND4z-T1xQkiGsVTIJF1ehvNGXUeh96R4Q4L28Mos41Oh1hU6SF9zjcGD6NTzdPzZe1DcUIxoZY3rK0woyLJva-h-WbntdVdfSQwqt7e8NQW2H9x2wKbbXrIDIRqPK185xVEdvs-SuyhyrXYCjBxA91zSX25ujvH0jj4Jnd7CCMt3ZfjPXJ60xvUrLaGwci1cGo5gu-LPozh8phvljzcsBONhHDZ1NlbCDCkGm612eQ86MILMayO_fuqMmtPJvXqf5KVOwXY1y4e4CeFPGnTB1TW6ujjYra2mvYihATuvqnYgFxGt_y9gUeEz8rlMC4zg898iTcggG5d65l-SLS0tUiOgnMkHGo'
                ];
            $request = new Request('GET',
                'https://pre-b.51kuafu.com/tiger/kms/api/task/shop/list?pageSize=2000&pageNum=1&type=' . $queryType,
                $headers);
            $res     = $client->sendAsync($request)->wait();
            $res     = json_decode($res->getBody()->getContents(), 1);
            $data    = $res['data']['list'];
            return $data;
        });

        $filterData = [];
        foreach ($data as $datum) {
            $filterData[] = ltrim($datum['shopCode'], 'KF');
        }
        sort($filterData);
        $field  = [
            '铺位号' => 'shopCode'
        ];
        $filter = [$gyFilterOne];
        $gyData = GuanyuanTool::fetch('d4e637ebdbb154560ad54721', 'c636d049c8ad6475987065db', $filter, 8, $field);

        foreach ($gyData as $datum) {
            $gyShopCodes[] = $datum['shopCode'];
        }


        $diff1 = array_diff($gyShopCodes, $filterData);
        $diff2 = array_diff($filterData, $gyShopCodes);

        $fsMessage = $title . '_差异数据' . PHP_EOL;
        $fsMessage .= '观远->运营差' . count($diff1) . '家' . PHP_EOL;
        foreach ($diff1 as $item) {
            $fsMessage .= $item . PHP_EOL;
        }
        $fsMessage .= '运营->观远差' . count($diff2) . '家' . PHP_EOL;
        foreach ($diff2 as $item) {
            $fsMessage .= $item . PHP_EOL;
        }

        $robot['urlToken'] = 'fa3b9aa6-fcf9-4315-beb6-6e439cce0288';
        $robot['secret']   = 'ktotrCJVtbfpbMGdudoJfc';
        Factory::feiShu()
               ->setToken($robot['urlToken'])
               ->setSecret($robot['secret'])
               ->setMessage(new TextMessage($fsMessage))
               ->send();
    }
}
